package com.tianji.promotion.service.impl;

import com.tianji.api.client.course.CategoryClient;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.promotion.domain.po.CouponScope;
import com.tianji.promotion.domain.vo.CouponScopeVO;
import com.tianji.promotion.mapper.CouponScopeMapper;
import com.tianji.promotion.service.ICouponScopeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券作用范围信息 服务实现类
 * </p>
 *
 * @author huanmo
 * @since 2025-09-26
 */
@Service
@RequiredArgsConstructor
public class CouponScopeServiceImpl extends ServiceImpl<CouponScopeMapper, CouponScope> implements ICouponScopeService {

    private final CategoryClient categoryClient;

    @Override
    public List<CouponScopeVO> queryCouponScopes(Long id) {
        List<CouponScope> scopes = lambdaQuery()
                .eq(CouponScope::getCouponId, id)
                .list();
        if (CollUtils.isEmpty(scopes)) {
            return Collections.emptyList();
        }
        // 转换为VO列表
        return scopes.stream()
                .map(this::convertToScopeVO)
                .collect(Collectors.toList());
    }

    private CouponScopeVO convertToScopeVO(CouponScope couponScope) {
        CouponScopeVO vo = new CouponScopeVO();
        vo.setId(couponScope.getBizId());
        vo.setName(categoryClient.get(vo.getId()).getName());
        return vo;
    }
}
